package com.databasedictionary.utils;


import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String 工具类
 * Created by sanhu on 2016/12/07.
 */
public class StringUtil {

    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static final String UTF8 = "UTF-8";
    private static String UNDEFINED = "undefined";
    private static String NULL = "null";

    /**
     * 生成6位数的邀请码
     */
    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        if (null == str || "".equals(str)) {
            return str;
        }
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);

        str = sb.toString();
        str = str.substring(0, 1).toUpperCase() + str.substring(1);

        return str;
    }

    /**
     * 驼峰转下划线,效率比上面高
     *
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线(简单写法，效率低于{@link #humpToLine(String)})
     *
     * @param str
     * @return
     */
    public static String humpToLine2(String str) {
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    /**
     * 首字母转小写
     *
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s) {
        if (StringUtil.isEmpty(s)) {
            return s;
        }
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 首字母转大写
     *
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s) {
        if (StringUtil.isEmpty(s)) {
            return s;
        }
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuffer()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * object转String
     *
     * @param object
     * @return
     */
    public static String getString(Object object) {
        return getString(object, "");
    }

    public static String getString(Object object, String defaultValue) {
        if (null == object) {
            return defaultValue;
        }
        try {
            return object.toString();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * object转Integer
     *
     * @param object
     * @return
     */
    public static int getInt(Object object) {
        return getInt(object, -1);
    }

    public static int getInt(Object object, Integer defaultValue) {
        if (null == object) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(object.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * object转Boolean
     *
     * @param object
     * @return
     */
    public static boolean getBoolean(Object object) {
        return getBoolean(object, false);
    }

    public static boolean getBoolean(Object object, Boolean defaultValue) {
        if (null == object) {
            return defaultValue;
        }
        try {
            return Boolean.parseBoolean(object.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * @param object
     * @return true=空,false=不为空;
     * @Description: 判断对象是否为空
     * @author zxn
     * @date 2016-11-6 下午02:16:17
     */
    public static boolean isEmpty(Object object) {
        boolean re = object == null || "".equals(object) || "null".equals(object.toString()) || "NULL".equals(object.toString()) || UNDEFINED.equals(object) || NULL.equals(object)
                || (object instanceof List && (((List<?>) object).size() == 0))
                || (object instanceof Map && (((Map<?, ?>) object).size() == 0));
        if (re) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean isNotEmpty(Object object) {
        return !isEmpty(object);
    }

    /**
     * @Copyright (C), 2017, 三虎科技有限公司
     * @Author: yanph
     * @category http请求utf8 编码2次
     * @Date: 2018/2/27 12:57
     * @History:
     */
    public static String httpStringUTF8(Object obj) {
        try {
            String strFrist = java.net.URLEncoder.encode(obj.toString(), UTF8);
            return java.net.URLEncoder.encode(strFrist, UTF8);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * <p>
     * 值  转换成固定个数
     * 不足前缀补  prefix
     * 超出取后面
     * 支持 null
     * </p>
     *
     * @param val
     * @param index
     * @param prefix
     * @return
     */
    public static String toFormatterStr(Object val, Integer index, String prefix) {
        String value = String.valueOf(val);
        StringBuilder sb = new StringBuilder(index);
        for (int i = value.length() - index; i < value.length(); i++) {
            sb.append(i > -1 ? value.charAt(i) : prefix);
        }
        return sb.toString();
    }

    /**
     * <p>
     * 值  转换成固定个数
     * 不足前缀补  0
     * 超出取后面
     * 支持 null
     * </p>
     *
     * @param val
     * @param index
     * @return
     */
    public static String toFormatterStr(Object val, Integer index) {
        return toFormatterStr(val, index, "0");
    }


    /**
     * 号段分别是移动，联通，电信；
     */
    private static String[] telFirst = ("134,135,136,137,138,139,147,148,150,151,152,157,158,159,172,178,182,183,184,187,188,198" +
            "130,131,132,145,146,155,156,166,171,175,176,185,186"
            + "133,149,153,173,174,177,180,181,189,199").split(",");

    /**
     * 获取随机手机号
     *
     * @param isShow 是否显示完全手机号码，true 显示完整手机号，false显示隐藏中间号码的手机号如186****5589
     * @return
     */
    public static String getRandomPhone(boolean isShow) {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = "";
        if (isShow) {
            second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        }
        String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        if (isShow) {
            return first + second + thrid;
        } else {
            return first + "****" + thrid;

        }
    }


    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    /**
     * 因此手机号中间4位数
     *
     * @param phone
     * @return
     */
    public static String hiddPhone(String phone) {
        return phone.substring(0, 3) + "********";
    }

    /**
     *
     * 获取随机 index 个数字 最大 max
     * @param index
     * @param max
     * @return
     */
    public static String getRandom(int index, int max) {
        Set<Integer> set = new HashSet(index);

        while (true){
            set.add(Double.valueOf(Math.random()*max+1).intValue());
            if(set.size()==index){
                break;
            }
        }
        ArrayList<Integer> integers = new ArrayList(set);
        Collections.sort(integers);
        String random = integers.toString();
        System.out.println(random);
        random = random.replace(" ","");
        return random.substring(1,random.length()-1);
    }


    public static void main(String[] args) {
        String random = getRandom(12, 18);
        System.out.println(random);
    }

}
